package com.xinqi.modules.stats.score.controller.web;

import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.stats.score.dto.req.*;
import com.xinqi.modules.stats.score.dto.rsp.CourseTotalScoreOfflineResultDTO;
import com.xinqi.modules.stats.score.dto.rsp.CourseTotalScoreOfflineStudentResultDTO;
import com.xinqi.modules.stats.score.dto.rsp.CourseTotalScoreStudentOfflineInfoResultDTO;
import com.xinqi.modules.stats.score.service.CourseTotalScoreOfflineService;
import com.xinqi.modules.stats.score.service.CourseTotalScoreOfflineStudentService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 线下成绩WEB控制层
 *
 * @author: dzy
 * @date: 2023/03/26
 */
@Tag(name = "线下成绩主表WEB控制层", description = "线下成绩主表WEB控制层")
@Validated
@RestController
@RequestMapping("/web/total-score-offline")
@RequiredArgsConstructor
public class CourseTotalScoreOfflineWebController {

    private final CourseTotalScoreOfflineService courseTotalScoreOfflineService;

    private final CourseTotalScoreOfflineStudentService courseTotalScoreOfflineStudentService;

    /**
     * 添加线下成绩
     * @author: dzy
     * @date: 2023/03/28
     */
    @PostMapping("/create")
    public R<String> create(@RequestBody CourseTotalScoreOfflineCreateDTO dto) {
        if (courseTotalScoreOfflineService.create(dto)) {
            return R.ok(MessageUtils.message("course.total.score.offline.create.success"));
        } else {
            return R.fail(MessageUtils.message("course.total.score.offline.create.fail"));
        }
    }

    /**
     * 修改线下成绩
     * @author: dzy
     * @date: 2023/03/28
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody CourseTotalScoreOfflineUpdateDTO dto) {
        if (courseTotalScoreOfflineService.updateById(dto)) {
            return R.ok(MessageUtils.message("course.total.score.offline.update.success"));
        } else {
            return R.fail(MessageUtils.message("course.total.score.offline.update.fail"));
        }
    }

    /**
     * 删除线下成绩
     * @author: dzy
     * @date: 2023/03/28
     */
    @DeleteMapping
    public R<String> delete(CourseTotalScoreOfflineDeleteDTO dto) {
        if (courseTotalScoreOfflineService.deleteById(dto)) {
            return R.ok(MessageUtils.message("course.total.score.offline.delete.success"));
        } else {
            return R.fail(MessageUtils.message("course.total.score.offline.delete.fail"));
        }
    }

    /**
     * 获取线下成绩列表
     * @author: dzy
     * @date: 2023/03/28
     */
    @GetMapping
    public R<List<CourseTotalScoreOfflineResultDTO>> find(CourseTotalScoreOfflineQueryDTO dto) {
        return R.ok(courseTotalScoreOfflineService.select(dto));
    }

    /**
     * 获取线下成绩列表-分页
     * @author: dzy
     * @date: 2023/03/28
     */
    @GetMapping("/page")
    public R<PageInfo<CourseTotalScoreOfflineResultDTO>> findPage(@Validated CourseTotalScoreOfflineQueryDTO dto) {
        return R.ok(courseTotalScoreOfflineService.selectPage(dto));
    }

    /**
     * 获取线下成绩学生列表
     * @author: dzy
     * @date: 2023/03/28
     */
    @GetMapping("/student")
    public R<List<CourseTotalScoreOfflineStudentResultDTO>> find(CourseTotalScoreOfflineStudentQueryDTO dto) {
        dto.setStudentId(null);
        return R.ok(courseTotalScoreOfflineService.selectStudentScore(dto));
    }

    /**
     * 给分学生线下成绩
     * @author: dzy
     * @date: 2023/03/28
     */
    @PutMapping()
    public R<String> update(@RequestBody CourseTotalScoreOfflineStudentUpdateDTO dto) {
        if (courseTotalScoreOfflineService.updateStudentScore(dto)) {
            return R.ok("给分成功");
        } else {
            return R.fail("给分失败");
        }
    }

    /**
     * 批量给分学生线下成绩
     * @author: dzy
     * @date: 2023/03/28
     */
    @PutMapping("/update/batch")
    public R<String> updateBatch(@RequestBody List<CourseTotalScoreOfflineStudentUpdateDTO> list) {
        if (courseTotalScoreOfflineService.updateBatchStudentScore(list)) {
            return R.ok("给分成功");
        } else {
            return R.fail("给分失败");
        }
    }

    /**
     * 学生获取自己的线下成绩
     * @author: dzy
     * @date: 2023/03/28
     */
    @GetMapping("/student/offline")
    public R<CourseTotalScoreStudentOfflineInfoResultDTO> studentOfflineScore(CourseTotalScoreOfflineStudentQueryDTO dto) {
        if (dto.getStudentId() == null) {
            dto.setStudentId(LoginHelper.getUserId());
        }
        return R.data(courseTotalScoreOfflineService.studentOfflineScore(dto));
    }

    /**
     * 学生获取自己的线下成绩-分页
     * @author: dzy
     * @date: 2023/03/28
     */
    @GetMapping("/student/offline/page")
    public R<PageInfo<CourseTotalScoreOfflineStudentResultDTO>> studentOfflineScorePage(CourseTotalScoreOfflineStudentQueryDTO dto) {
        if (dto.getStudentId() == null) {
            dto.setStudentId(LoginHelper.getUserId());
        }
        return R.data(courseTotalScoreOfflineService.studentOfflineScorePage(dto));
    }

    /**
     * 学生获取自己线下成绩未读数量
     * @author: dzy
     * @date: 2023/03/28
     */
    @GetMapping("/student/unread")
    public R<Long> studentUnRead(CourseContentTotalScoreClassDTO dto) {
        dto.setStudentId(LoginHelper.getUserId());
        return R.data(courseTotalScoreOfflineStudentService.studentUnRead(dto));
    }

    /**
     * 学生更新为已读标志
     * @author: dzy
     * @date: 2023/03/28
     */
    @GetMapping("/student/read")
    public R<String> studentRead(CourseContentTotalScoreClassDTO dto) {
        dto.setStudentId(LoginHelper.getUserId());
        courseTotalScoreOfflineStudentService.studentReadOffline(dto);
        return R.ok("设置已读成功");
    }

}
